package dao;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import entities.Locate;
/*
 * quan ly 2 file bookmark.bin va recent.bin de luu tru bookkmark va recent locate
 */
public class FileBookmarkRecent{
	public static String BOOKMARK_FILE="bookmarkCF.bin";
	public static String RECENT_FILE="recentCF.bin";
	private String fileName;
	private Context context;
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public FileBookmarkRecent(String fileName,Context context) {
		super();
		this.fileName = fileName;
		this.context=context;

	}
	public Boolean locateHasAdded(Locate locate){
		Boolean flag=false;
		ArrayList<Locate> list=loadAll();
		if(list!=null&&list.size()>0){
			for(Locate locate2:list)
				if(locate2.getId()==locate.getId())
				{
					flag=true;
					break;
				}
		}
		return flag;
	}
	public Boolean add(Locate locate2){
		Locate locate=new Locate(locate2);
		if(!locateHasAdded(locate)){
			locate.getImages().clear();
			FileOutputStream fo;
			try {
		   		ArrayList<Locate> listBookmarked=new ArrayList<Locate>(loadAll());
		   		fo = context.openFileOutput(fileName,Context.MODE_WORLD_WRITEABLE);
				BufferedOutputStream bo=new BufferedOutputStream(fo);
		   		ObjectOutputStream obo=new ObjectOutputStream(bo);
		   		if(listBookmarked.size()>0)
		   		{
		   			for(Locate l:listBookmarked)
		   			{
		   				obo.writeObject(l);
		   			}
		   		}
		   		
		   		obo.writeObject(locate);
		        obo.close();
		        bo.close();
		        return true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
	public ArrayList<Locate> loadAll(){
		ArrayList<Locate> listLocate=new ArrayList<Locate>();
		try {
			
			FileInputStream fi=context.openFileInput(fileName);
			BufferedInputStream bi=new BufferedInputStream(fi);
			ObjectInputStream oi=new ObjectInputStream(bi);
			Locate locate;
			while(bi.available()>0){
				Log.d("one bookmark","-------------");
				locate=(Locate) oi.readObject();
				if(locate!=null)
					listLocate.add(locate);
			}
			oi.close();
			bi.close();
			fi.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return listLocate;
	}
	public Boolean deleteAll(){
		FileOutputStream fo;
		try {
			fo = context.openFileOutput(fileName,Context.MODE_WORLD_WRITEABLE);
			BufferedOutputStream bo=new BufferedOutputStream(fo);
	   		ObjectOutputStream obo=new ObjectOutputStream(bo);
	   		obo.close();
	        bo.close();
	   		return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public Boolean delete(Locate locate){
		ArrayList<Locate> templist=new ArrayList<Locate>(loadAll());
		deleteAll();
		for(Locate locate2:templist){
			if(locate.getId()!=locate2.getId())
				add(locate2);
		}
		return true;
	}
	public Boolean deleteFirstItem(){
		ArrayList<Locate> list=loadAll();
		if(list.size()>0){
			Locate locate=list.get(0);
			delete(locate);
			return true;
		}
		return false;
	}
}
